# -*- coding: utf-8 -*-
import gtk
from time import strftime
from sugar.activity import activity

masculin_data = {6:14.54, 7:15.07, 8:15.62, 9:16.17, 10:16.72, 12:17.87, 13:18.53, 14:19.22,
					15:19.92, 16:20.63, 17:21.12, 18:21.45, 19:21.86, 20:23.07, 21:23.07, 22:23.07,
					23:23.07, 24:23.07, 25:24.19, 26:24.19, 27:24.19, 28:24.19, 29:24.19, 30:24.90,
					31:24.90, 32:24.90, 33:24.90, 34:24.90, 35:25.25, 36:25.25, 37:25.25, 38:25.25,
					39:25.25, 40:25.49, 41:25.49, 42:25.49, 43:25.49, 44:25.49, 45:25.55, 46:25.55, 47:25.55, 48:25.55, 49:25.55, 50:25.54, 51:25.54, 52:25.54, 53:25.54,
					54:25.54, 55:25.51, 56:25.51, 57:25.51, 58:25.51, 59:25.51, 60:25.47, 61:25.47,
					62:25.47, 63:25.47, 64:25.47, 65:25.41, 66:25.41, 67:25.41, 68:25.41, 69:25.41,
					70:25.33, 71:25.33, 72:25.33, 73:25.33, 74:25.33}
fem_data = {6:14.31, 7:14.98, 8:15.66, 9:16.33, 10:17.00, 11:17.67, 12:18.35, 13:18.95, 14:19.32,
					15:19.69, 16:20.09, 17:20.36, 18:20.57, 19:20.80, 20:21.45, 21:21.45, 22:21.45,
					23:21.45, 24:21.45, 25:22.10, 26:22.10, 27:22.10, 28:22.10, 29:22.10, 30:22.69, 
					31:22.69, 32:22.69, 33:22.69, 34:22.69, 35:23.25, 36:23.25, 37:23.25, 38:23.25,
					39:23.25, 40:23.74, 41:23.74, 42:23.74, 43:23.74, 44:23.74, 45:24.17, 46:24.17,
					47:24.17, 48:24.17, 49:24.17, 50:24.54, 51:24.54, 52:24.54, 53:24.54, 54:24.54,
					55:24.92, 56:24.92, 57:24.92, 58:24.92, 59:24.92, 60:25.29, 61:25.29, 62:25.29,
					63:25.29, 64:25.29, 65:25.66, 66:25.66, 67:25.66, 68:25.66, 69:25.66, 70:26.07,
					71:26.07, 72:26.07, 73:26.07, 74:26.07}

class Imc(activity.Activity):
	def __init__(self, handle):
		"""This class builds the main window and its components

        Keyword arguments:
        self -- the Interface object (GtkDrawingArea)

        """
		activity.Activity.__init__(self, handle)
		self.set_title('IMC')

		# Fixed, the main container
		self.fixed = gtk.Fixed()
		self.add(self.fixed)
		self.fixed.show()

		# 6 rows
		self.rows = gtk.Table(6,1,True)
		self.fixed.put(self.rows,0,0)
		self.rows.show()

		# First row: Title
		self.banner = gtk.Label()
		self.banner.set_markup("<span font_family='arial' size='20000' color = '#0f70e8'><b>Í</b>ndice de <b>M</b>assa <b>C</b>orporal</span>")
		self.rows.attach(self.banner, 0, 1, 0, 1)
		self.banner.show()

		# Second row: Name and Date
		self.name_label = gtk.Label()
		self.name_label.set_markup("<span font_family='arial' size='10000' color = '#0f70e8'><b>Nome Completo</b></span>")
		self.name_entry = gtk.Entry()
		self.date_label = gtk.Label()
		self.date_label.set_markup("<span font_family='arial' size='10000' color = '#0f70e8'>Data: %s</span>" % strftime("%d/%m/%Y"))
		self.second_box = gtk.HBox(False)
		self.second_box.pack_start(self.name_label)
		self.second_box.pack_start(self.name_entry)
		self.second_box.pack_start(self.date_label)
		self.rows.attach(self.second_box, 0, 1, 1, 2)
		self.second_box.show_all()

		# Third row: Age, Sex
		self.age_label = gtk.Label()
		self.age_label.set_markup("<span font_family='arial' size='10000' color = '#0f70e8'><b>Idade (anos):</b></span>")
		self.age_entry = gtk.Entry(2)
		self.age_entry.set_size_request(50,25)
		self.sex_label = gtk.Label()
		self.sex_label.set_markup("<span font_family='arial' size='10000' color = '#0f70e8'><b>Sexo:</b></span>")
		self.masc_button = gtk.RadioButton(None, "Masculino")
		self.fem_button = gtk.RadioButton(self.masc_button, "Feminino")
		self.third_box = gtk.HBox(False)
		self.third_box.pack_start(self.age_label)
		self.third_box.pack_start(self.age_entry)
		self.third_box.pack_start(self.sex_label)
		self.third_box.pack_start(self.masc_button)
		self.third_box.pack_start(self.fem_button)
		self.rows.attach(self.third_box, 0, 1, 2, 3)
		self.third_box.show_all()

		# Forth row: weight, height
		self.weight_label = gtk.Label()
		self.weight_label.set_markup("<span font_family='arial' size='10000' color = '#0f70e8'><b>Peso (kg):</b></span>")
		self.weight_entry = gtk.Entry(6)
		self.weight_entry.set_size_request(50,25)
		self.height_label = gtk.Label()
		self.height_label.set_markup("<span font_family='arial' size='10000' color = '#0f70e8'><b>Estatura (cm):</b></span>")
		self.height_entry = gtk.Entry(6)
		self.height_entry.set_size_request(50,25)
		self.fourth_box = gtk.HBox(False)
		self.fourth_box.pack_start(self.weight_label)
		self.fourth_box.pack_start(self.weight_entry)
		self.fourth_box.pack_start(self.height_label)
		self.fourth_box.pack_start(self.height_entry)
		self.rows.attach(self.fourth_box, 0, 1, 3, 4)
		self.fourth_box.show_all()


		# Fifth row: calculate
		fixed = gtk.Fixed()
		self.rows.attach(fixed, 0, 1, 4, 5)
		self.calc_button = gtk.Button("Calcular")
		self.calc_button.connect("clicked", self.calcula_imc)
		self.calc_button.set_size_request(100,50)
		fixed.put(self.calc_button,560,20)
		fixed.show_all()

		# Sixth row: result
		vbox = gtk.VBox()
		self.result = gtk.Label()
		self.imc = gtk.Label()
		self.status = gtk.Label()
		vbox.pack_start(self.result)
		vbox.pack_start(self.imc)
		vbox.pack_start(self.status)
		self.rows.attach(vbox, 0, 1, 5, 6)
		vbox.show_all()

		self.show()

	def destroy(self, *arg):
		gtk.main_quit()

	def calcula_imc(self, *args):
		try:
			a = int(self.age_entry.get_text())
			try: masculin_data[a]
			except:fem_data[a]
		except:
			self.result.set_markup("<span font_family='arial' size='10000' color = '#0f70e8'><b>Você não digitou a Idade corretamente.</b></span>")
			return None
		try: w = float(self.weight_entry.get_text().replace(',','.'))
		except:
			self.result.set_markup("<span font_family='arial' size='10000' color = '#0f70e8'><b>Você não digitou o Peso corretamente.</b></span>")
			return None
		try: h = float(self.height_entry.get_text().replace(',','.'))
		except:
			self.result.set_markup("<span font_family='arial' size='10000' color = '#0f70e8'><b>Você não digitou a Estatura corretamente.</b></span>")
			return None
		if self.masc_button.get_active(): imc_50 = masculin_data[a]
		else: imc_50 = fem_data[a]
		user_imc = 10000*w/(h*h)
		percentage = 100*user_imc/imc_50
		if percentage > 160: status = "Obesidade de Alto Risco"
		elif percentage > 139: status = "Obesidade de Grau 3"
		elif percentage > 129: status = "Obesidade de Grau 2"
		elif percentage > 119: status = "Obesidade de Grau 1"
		elif percentage > 79: status = "Adequado"
		else: status = "Abaixo"
		self.result.set_markup("<span font_family='arial' size='10000' color = '#0f70e8'><b>Resultado</b></span>")
		self.imc.set_markup("<span font_family='arial' size='10000' color = '#0f70e8'><b>IMC - obtido = %.2f\t\tIMC - perc 50: %.2f\tAdequação do IMC = %.2f %s</b></span>" % (user_imc, imc_50,percentage,"%"))
		self.status.set_markup("<span font_family='arial' size='10000' color = '#0f70e8'><b>\tClassificação = %s</b></span>" % status)
